package com.alibaba.otter.manager.web.service;

import com.alibaba.otter.manager.biz.config.alarm.AlarmRuleService;
import com.alibaba.otter.manager.biz.config.channel.ChannelService;
import com.alibaba.otter.manager.biz.config.pipeline.PipelineService;
import com.alibaba.otter.manager.biz.statistics.delay.DelayStatService;
import com.alibaba.otter.manager.biz.statistics.throughput.ThroughputStatService;
import com.alibaba.otter.manager.biz.statistics.throughput.param.ThroughputCondition;
import com.alibaba.otter.manager.web.dto.PipelineInfoDTO;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.manager.web.mapstruct.PipelineMapper;
import com.alibaba.otter.shared.arbitrate.ArbitrateViewService;
import com.alibaba.otter.shared.arbitrate.model.PositionEventData;
import com.alibaba.otter.shared.common.model.config.alarm.AlarmRule;
import com.alibaba.otter.shared.common.model.config.channel.Channel;
import com.alibaba.otter.shared.common.model.config.node.Node;
import com.alibaba.otter.shared.common.model.config.pipeline.Pipeline;
import com.alibaba.otter.shared.common.model.statistics.delay.DelayStat;
import com.alibaba.otter.shared.common.model.statistics.throughput.ThroughputStat;
import com.alibaba.otter.shared.common.model.statistics.throughput.ThroughputType;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@Service
public class PipelineInfoAggService {


    private final DelayStatService delayStatService;

    private final ChannelService channelService;

    private final ThroughputStatService throughputStatService;

    private final AlarmRuleService alarmRuleService;

    private final ArbitrateViewService arbitrateViewService;

    private final PipelineService pipelineService;

    private final PipelineMapper pipelineMapper;

    public PipelineInfoAggService(DelayStatService delayStatService,
                                  ChannelService channelService,
                                  ThroughputStatService throughputStatService,
                                  AlarmRuleService alarmRuleService,
                                  ArbitrateViewService arbitrateViewService,
                                  PipelineService pipelineService,
                                  PipelineMapper pipelineMapper) {
        this.delayStatService = delayStatService;
        this.channelService = channelService;
        this.throughputStatService = throughputStatService;
        this.alarmRuleService = alarmRuleService;
        this.arbitrateViewService = arbitrateViewService;
        this.pipelineService = pipelineService;
        this.pipelineMapper = pipelineMapper;
    }


    public void create(Pipeline node) {
        // select/extract节点普遍配置为同一个节点
        List<Node> selectNodes = node.getSelectNodes();
        node.setExtractNodes(selectNodes);
        pipelineService.create(node);
    }

    public void modify(Pipeline node) {
        List<Node> selectNodes = node.getSelectNodes();
        node.setExtractNodes(selectNodes);
        pipelineService.modify(node);
    }


    public List<PipelineInfoDTO> pipelineInfo(Long channelId, Long pipelineId){

        Channel channel = channelService.findByIdWithoutColumn(channelId);
        List<Pipeline> pipelines = channel.getPipelines();
        List<Pipeline> tempPipe = new ArrayList<>();

        if ((pipelineId != null) && (pipelineId != 0L)) {
            for (Pipeline pipeline : pipelines) {
                if (!pipeline.getId().equals(pipelineId)) {
                    tempPipe.add(pipeline);
                }
            }
            pipelines.removeAll(tempPipe);
        }

        List<PipelineInfoDTO> pipelineInfos = pipelineMapper.toDTO(pipelines);
        for (PipelineInfoDTO pipeline : pipelineInfos) {
            DelayStat delayStat = delayStatService.findRealtimeDelayStat(pipeline.getId());
            if (delayStat.getDelayNumber() == null) {
                delayStat.setDelayNumber(0L);
                delayStat.setDelayTime(0L);
                delayStat.setGmtModified(pipeline.getGmtModified());
            }
            pipeline.setDelayStat(delayStat);
            pipeline.setMainStem( arbitrateViewService.mainstemData(channel.getId(), pipeline.getId()));
            ThroughputCondition condition = new ThroughputCondition();
            condition.setPipelineId(pipeline.getId());
            condition.setType(ThroughputType.ROW);
            ThroughputStat throughputStat = throughputStatService.findThroughputStatByPipelineId(condition);
            pipeline.setThroughput( throughputStat);
            List<AlarmRule> alarmRules = alarmRuleService.getAlarmRules(pipeline.getId());
            pipeline.setAlarmRules( alarmRules);
            PositionEventData positionData = arbitrateViewService.getCanalCursor(pipeline.getParameters().getDestinationName(),
                    pipeline.getParameters().getMainstemClientId());
            pipeline.setPosition( positionData);
        }

        return pipelineInfos;
    }
}
